package com.test.testokhttp;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * 作者： andli
 * 时间： 2017/9/30 13:54
 * 描述： GET请求
 */
public class Send_Get_Activity extends AppCompatActivity {

    private OkHttpClient okHttpClient;
    private TextView resultTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_get_example);

        setTitle("Get请求的用法");

        okHttpClient = new OkHttpClient();
        resultTextView = (TextView) findViewById(R.id.result_textview);
    }

    /**
     * 简单的Get请求（不带参数）
     *
     * @param view
     */
    public void simpleGetClick(View view) {

        // http://192.168.199.225:8888/okhttp/test_simple_get.php

        String url = API.Base_URL + "test_simple_get.php";
        Log.i("andli", "接口地址：" + url);

        Request request = new Request.Builder()
                .url(url)
                .build();

        okHttpClient.newCall(request).enqueue(callback);
    }

    /**
     * 简单的Get请求（带参数）
     *
     * @param view
     */
    public void addParamGetClick(View view) {

        // http://192.168.199.225:8888/okhttp/test_param_get.php?username=zhangsan&phone=13888888888

        String url = API.Base_URL + "test_param_get.php?username=zhangsan&phone=13888888888"; // 携带参数
        Log.i("andli", "接口地址：" + url);

        Request request = new Request.Builder()
                .addHeader("token", "asdlfjkasdljfaskdjfalsjkljalk")  // 请求头中加入参数
                .url(url)
                .build();
        okHttpClient.newCall(request).enqueue(callback);
    }

    /**
     * 接口请求 回调处理
     */
    private Callback callback = new Callback() {
        @Override
        public void onFailure(Call call, IOException e) {
            setResult(e.getMessage(), false);
        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            setResult(response.body().string(), true);
        }
    };

    /**
     * 显示 返回结果
     *
     * @param msg
     * @param success
     */
    private void setResult(final String msg, final boolean success) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (success) {
                    Toast.makeText(Send_Get_Activity.this, "请求成功", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(Send_Get_Activity.this, "请求失败", Toast.LENGTH_SHORT).show();
                }
                resultTextView.setText(msg);
                Log.i("andli", "返回参数：" + resultTextView.getText().toString());
            }
        });
    }

}
